/**
 * 
 */
package temp.helloworld.diiop;

import temp.helloworld.log.LoggerHelper;
import temp.helloworld.properties.PropertiesHelper;
import lotus.domino.Database;
import lotus.domino.Document;
import lotus.domino.NotesError;
import lotus.domino.NotesException;
import lotus.domino.NotesFactory;
import lotus.domino.Session;

/**
 * @author admin
 * 
 */
public class DIIOPHelper {
	public static String getStrHostName() {
		return PropertiesHelper.getProperty("diiop.address");
	}

	public static String getStrUser() {
		return PropertiesHelper.getProperty("diiop.user");
	}

	public static String getStrPwd() {
		return PropertiesHelper.getProperty("diiop.pwd");
	}

	/**
	 * 获得Domino进程
	 * 
	 * @param strHostName
	 * @param strDIIOPPort
	 * @param strUser
	 * @param strPwd
	 * @return Domino进程
	 */
	public static Session createSessionWithUsername(String strUser,
			String strPwd) {
		return createSession(getStrHostName(), strUser, strPwd);
	}

	/**
	 * 获得Domino进程
	 * 
	 * @return
	 */
	public static Session createSession() {
		return createSession(getStrHostName(), getStrUser(), getStrPwd());
	}

	/**
	 * 获得Domino进程
	 * 
	 * @return
	 */
	public static Session createSessionAsSigner() {
		return createSession(getStrHostName(), getStrUser(), getStrPwd());
	}

	/**
	 * 获得Domino进程
	 * 
	 * @return
	 */
	public static Session createSessionAsSignerWithFullAccess() {
		return createSession(getStrHostName(), getStrUser(), getStrPwd());
	}

	/**
	 * 根据用户名、密码通过DIIOP创建Domino进程
	 * 
	 * @param strHostName
	 * @param strDIIOPPort
	 * @param strUser
	 * @param strPwd
	 * @return Domino进程
	 */
	public static Session createSession(String strHostName, String strUser,
			String strPwd) {
		try {
			return NotesFactory.createSession(strHostName, strUser, strPwd);
		} catch (NotesException e) {
			String dominoErrorText = e.text;
			int dominoErrorID = e.id;

			switch (dominoErrorID) {
			case NotesError.NOTES_ERR_INVALID_USERNAME_PASSWD:
				// Invalid password.
				LoggerHelper.logger.error("This password, " + strPwd
						+ ", is not valid");
				break;
			case NotesError.NOTES_ERR_INVALID_USERNAME:
				// Invalid username.
				LoggerHelper.logger.error("This user, " + strUser
						+ ", is not valid");
				break;
			case NotesError.NOTES_ERR_SERVER_ACCESS_DENIED:
				// Access denied.
				LoggerHelper.logger
						.error("This user, "
								+ strUser
								+ ", is not authorized to open DIIOP connections with the Domino server.  Check your DIIOP configuration.  NotesException - "
								+ dominoErrorID + " " + dominoErrorText);
				break;
			case NotesError.NOTES_ERR_GETIOR_FAILED:
				// Could not get IOR from Domino Server.
				LoggerHelper.logger
						.error("Unable to open a DIIOP connection with "
								+ strHostName
								+ ".  Make sure the DIIOP and HTTP tasks are running on the Domino server, and that ports are open.  NotesException - "
								+ dominoErrorID + " " + dominoErrorText + ".");
				break;
			default:
				// Unexpected error. Show detailed message.
				LoggerHelper.logger.error("NotesException - " + dominoErrorID
						+ " " + dominoErrorText, e);
			}

		} catch (Exception e) {
			LoggerHelper.logger.error("", e);
		}
		return null;
	}

	/**
	 * 根据ltpaToken通过DIIOP创建Domino进程
	 * 
	 * @param strHostName
	 * @param strDIIOPPort
	 * @param strUser
	 * @param strPwd
	 * @return Domino进程
	 */
	public static Session createSession(String strHostName,
			String ltpaTokenString) {
		try {
			return NotesFactory.createSession(strHostName, ltpaTokenString);
		} catch (NotesException e) {
			String dominoErrorText = e.text;
			int dominoErrorID = e.id;

			switch (dominoErrorID) {
			case NotesError.NOTES_ERR_SSOTOKEN_EXP:
				// Single Sign-on Token has expired.
				LoggerHelper.logger.error("NotesException - " + dominoErrorID
						+ " " + dominoErrorText);
				break;
			case NotesError.NOTES_ERR_SERVER_ACCESS_DENIED:
				// Access denied.
				LoggerHelper.logger
						.error("This user is not authorized to open DIIOP connections with the Domino server.  Check your DIIOP configuration.  NotesException - "
								+ dominoErrorID + " " + dominoErrorText);
				break;
			case NotesError.NOTES_ERR_GETIOR_FAILED:
				// Could not get IOR from Domino Server.
				LoggerHelper.logger
						.error("Unable to open a DIIOP connection with "
								+ strHostName
								+ ".  Make sure the DIIOP and HTTP tasks are running on the Domino server, and that ports are open.  NotesException - "
								+ dominoErrorID + " " + dominoErrorText + ".");
				break;
			default:
				// Unexpected error. Show detailed message.
				LoggerHelper.logger.error("NotesException - " + dominoErrorID
						+ " " + dominoErrorText, e);

			}

		} catch (Exception e) {
			LoggerHelper.logger.error("", e);
		}
		return null;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Session session = DIIOPHelper.createSession();
		try {
			System.out.println(session.getCommonUserName());
			Database db = session.getDatabase(session.getServerName(),
					"names.nsf");

			Document doc = db.createDocument();
			doc.replaceItemValue("Form", "Memo");
			doc.replaceItemValue("Subject", "test mail");
			doc.send("Adminshshyjyn/上海石化研究院/sinopec");
		} catch (NotesException e) {
			e.printStackTrace();
		}
	}
}
